package it.tukano.treditor.ui;

import it.tukano.collections.LinkedListExt;
import it.tukano.collections.ListExt;
import it.tukano.swingdeco.containers.JPanelExt;
import it.tukano.swingdeco.controls.JSpinnerExt;
import it.tukano.treditor.engine.geom.EulerSet;
import java.awt.GridLayout;

public class EulerSetEditor extends JPanelExt {
    
    /* default serial version uid*/
    private static final long serialVersionUID = 0;
    
    private final JSpinnerExt x = new JSpinnerExt();
    private final JSpinnerExt y = new JSpinnerExt();
    private final JSpinnerExt z = new JSpinnerExt();
    private final ListExt<Runnable> listeners = new LinkedListExt<>();

    /**
     * Instance initializer
     */
    public EulerSetEditor(String titlex, String titley, String titlez, Number step) {
        super(new GridLayout(1, 3));
        appendWithTitles(titlex, x, titley, y, titlez, z);
        final Runnable spinnerListener = new Runnable() {

            public void run() {
                for (Runnable r : listeners) {
                    r.run();
                }
            }
        };
        x.withNumericModel(0.0, step);
        y.withNumericModel(0.0, step);
        z.withNumericModel(0.0, step);
        x.addListener(spinnerListener);
        y.addListener(spinnerListener);
        z.addListener(spinnerListener);
    }
    
    public void addListener(Runnable listener) {
        listeners.add(listener);
    }
    
    public void setEditedValue(EulerSet t) {
        t = t.toDegrees();
        listeners.cacheAndClear();
        x.setValue(t.getX());
        y.setValue(t.getY());
        z.setValue(t.getZ());
        listeners.restoreCache();
    }
    
    public EulerSet getEditedValue() {
        return EulerSet.newDegreesInstance(x.getNumberValue(), y.getNumberValue(), z.getNumberValue());
    }
}
